package com.example.Utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;
import java.util.Map;

/**
 * ClassName:jwtUtils
 * Description:
 *
 * @Author 赵永峰
 * @Create 2024/5/10 7:47
 * @Version 1.0
 */
public class jwtUtils {
    private static String signkey = "itzhaoyongfeng";
    private static Long expire = 43200000000L;
    /**
    * 生成JWT令牌
     */
    public static String generateJwt(Map<String,Object> claims){
        String jwt = Jwts.builder()
                .addClaims(claims)
                .signWith(SignatureAlgorithm.HS256,signkey)
                .setExpiration(new Date(System.currentTimeMillis()+expire))
                .compact();
        return jwt;
    }

    /*
    * 解析JWT令牌
    */
    public static Claims parseJWT(String jwt){
        Claims claims = Jwts.parser()
                .setSigningKey(signkey)
                .parseClaimsJws(jwt)
                .getBody();
        return claims;
    }
}
